<?php

namespace Model\SchoolCategoryBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

use Symfony\Component\HttpFoundation\Request;
use Base\BaseController;
use Model\SchoolCategoryBundle\Entity\SchoolCategory as Entity;
use Model\SchoolCategoryBundle\Setting\SchoolCategorySetting;

/**
 * @route("/admin/")
 */
class SchoolCategoryAdminController extends BaseController
{

    /**
     * @route("SchoolCategory/add",name="SchoolCategoryPreAdd")
     */
    public function add()
    {
        //私有代码
        if ( $parent = $this->getParamRequired("parent")) {
            $entity = new Entity();
            $entity->setParent($parent);
        }else{
            $entity = null;
        }
        //通用代码
        $this->setSetting(new SchoolCategorySetting($this->getDoctrine()));
        return $this->comAdd($entity);
    }

    /**
     * @route("SchoolCategory/add/do",name="SchoolCategoryDoAdd")
     *
     * @Method({"GET", "POST"})
     */
    public function doAdd(Request $request)
    {
        //私有代码
        $entity = new Entity();
        $parent = $this->getParamRequired("form");
        $rep = $this->getDoctrine()->getRepository("ModelSchoolCategoryBundle:SchoolCategory");
        $parentEntity = $rep->find($parent["parent"]);
        if ($parentEntity) {
            $level = $parentEntity->getLevel() + 1;
        } else {
            $level = 0;
        }
        $entity->setLevel($level);

        //通用代码
        $this->setSetting(new SchoolCategorySetting($this->getDoctrine()));
        return $this->comDoAdd($request,$entity);
    }

    /**
     * @route("SchoolCategory/edit/{id}",name="SchoolCategoryPreEdit")
     */
    public function edit(Entity $entity)
    {
        $this->setSetting(new SchoolCategorySetting($this->getDoctrine()));
        return $this->comEdit($entity);
    }

    /**
     * @route("SchoolCategory/edit/{id}/do",name="SchoolCategoryDoEdit")
     *
     * @Method({"GET", "POST"})
     */
    public function doEdit(Request $request,Entity $entity)
    {
        //私有代码
        $parent = $entity->getParent();
        $rep = $this->getDoctrine()->getRepository("ModelSchoolCategoryBundle:SchoolCategory");
        $parentEntity = $rep->find($parent);
        if ($parentEntity) {
            $level = $parentEntity->getLevel() + 1;
        } else {
            $level = 0;
        }
        $entity->setLevel($level);

        $this->setSetting(new SchoolCategorySetting($this->getDoctrine()));
        return $this->comDoEdit($request,$entity);
    }

    /**
     * @route("SchoolCategory",name="SchoolCategoryLists")
     */
    public function lists()
    {
        $this->setSetting(new SchoolCategorySetting($this->getDoctrine()));
        return $this->comCatLists();
    }


    /**
     * @route("SchoolCategory/tools",name="SchoolCategoryTools")
     */
    public function tools(Request $request)
    {
        $this->setSetting(new SchoolCategorySetting($this->getDoctrine()));
        return $this->comTools($request);
    }

}
